package com.hao.study.service.impl;

import com.hao.study.mapper.ErrorLogMapper;
import com.hao.study.service.ErrorLogCleanupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;

/**
 * @author: author
 * @description:
 * @date: 2024/6/14 11:42
 * @version: 1.0
 */
@Service
public class ErrorLogCleanupServiceImpl implements ErrorLogCleanupService {
    @Autowired
    private ErrorLogMapper mapper; // 假设这是操作日志表的Repository

    @Override
    public void cleanupErrorLogs(int daysBefore) {
        // 获取当前时间的UTC日期
        LocalDate today = LocalDate.now(ZoneId.of("UTC"));
        // 计算30天前的日期
        LocalDate dateBefore = today.minusDays(daysBefore);

        // 获取30天前的起始时间（当天0点）
        ZonedDateTime startOfDayUTC = dateBefore.atStartOfDay(ZoneId.of("UTC"));
        long timestampBefore = startOfDayUTC.toInstant().toEpochMilli();
        mapper.deleteByLogTimeBefore(timestampBefore);
    }
}
